﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Threading;

public partial class 多线程_ThreadMore : System.Web.UI.Page
{
    Linq查询DataContext DB = new Linq查询DataContext();
    public void iiiooo()
    {
        for (int i = 0; i < 999; i++)
        {
            Response.Write(i + "<br/>\r\n");
        }

    }

    public void GetStr(object obj)
    {
        int j=0;
        string uu = (string)obj;

        while (j < 99999)
        {
            //Response.Write(j + "<br>");
            j++;
        }
        Response.Write(uu);
        oo = true;// 关闭线程
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime d = DateTime.Now;

       
        
       //ThreadStart Tparame = new ThreadStart(iiiooo);
       // Thread Thr = new Thread(Tparame);
       // Thr.Start();


        //调用带参数的方法 不能有返回值
        //ParameterizedThreadStart TheradObj = new ParameterizedThreadStart(GetStr);
        //Thread T = new Thread(TheradObj);
        //T.Start("多多");
        //T.Name = "大";
        //T.Join();

        ///使用多线程处理数据
        int j = 0;
        while (j < 999)
        {
            Thread thS = new Thread(GetStr);
            thS.Start("当前+: " + j);
            thS.Join();

            //j++;
            //Thread thS1 = new Thread(GetStr);
            //thS1.Start("当前+: " + j);
            //thS1.Join();

            //j++;
            //Thread thS2 = new Thread(GetStr);
            //thS2.Start("当前+: " + j);
            //thS2.Join();

            //j++;
            //Thread thS3 = new Thread(GetStr);
            //thS3.Start("当前+: " + j);
            //thS3.Join();

            j++;
        }


        Response.Write("<br/>"+(DateTime.Now.Second - d.Second));
        //线程的关闭
    }
    public volatile bool oo = false;
}


public class Thread_Class
{
    

}


/// <summary>
/// use more Threads
/// </summary>
public  class  Thread_Test
{
    #region initialze some parnames
    private bool canUse = true;// initialze use more Threads

    /// <summary>
    /// 初始化 是不是用多线程
    /// initialze more Threads
    /// </summary>
    public bool CanUse
    {
        get { return canUse; }
        set { canUse = value; }
    }


    /// <summary>
    /// 
    /// </summary>
    private int getCurrentThreads = 0;
    /// <summary>
    ///初始化当前的线数
    /// get Current Therad's count
    /// </summary>
    public int GetCurrentThreads
    {
        get { return getCurrentThreads; }
        set { getCurrentThreads = value; }
    }

    /// <summary>
    /// get error messages
    /// </summary>
    private string errorText = "";

    /// <summary>
    /// 出错信息
    /// get error messages
    /// </summary>
    public string ErrorText
    {
        get { return errorText; }
        set { errorText = value; }
    }
     



#endregion

    #region initialze struct Function
    public Thread_Test()
    { 
        
    }

    /// <summary>
    /// 初始会是不是使用多线程
    /// initialze can use Threads
    /// </summary>
    /// <param name="Cnause"></param>
    public Thread_Test(bool Cnause)
    {
        CanUse = canUse;
    }

    #endregion

    /*
     
     一个进程可以创建一个或多个线程以执行与该进程关联的部分程序代码。使用 ThreadStart 委托或 ParameterizedThreadStart 委托指定由线程执行的程序代码。使用 ParameterizedThreadStart 委托可以将数据传递到线程过程。

在线程存在期间，它总是处于由 ThreadState 定义的一个或多个状态中。可以为线程请求由 ThreadPriority 定义的调度优先级，但不能保证操作系统会接受该优先级。

GetHashCode 提供托管线程的标识。在线程的生存期内，无论获取该值的应用程序域如何，它都不会和任何来自其他线程的值冲突。

http://msdn2.microsoft.com/zh-cn/library/system.threading.thread(VS.80).aspx
     */
    /// <summary>
    /// 创建新线程 处理传过来的 处理方法
    /// 
    /// create New Thread deal whit pass Function 
    /// </summary>
    /// <param name="TheradName">线程名,Therad Name</param>
    /// <returns></returns>
    public string CreateNewTheradWithVoidFunction(ThreadStart DealWithFunction)
    {
        Thread Ther = new Thread(new ThreadStart(DealWithFunction));
        getCurrentThreads++;
        Ther.Start();
        Ther.Name = "000";
        
        return Ther.ThreadState.ToString();
        
    }
}




/*如何：创建和终止线程（C# 编程指南）
*/
public class Worker: System.Web.UI.Page
{
    // This method will be called when the thread is started.
    public void DoWork()
    {
        while (!_shouldStop)
        {
            Response.Write("worker thread: working...");
        }
        Response.Write("worker thread: terminating gracefully.");
    }
    public void RequestStop()
    {
        _shouldStop = true;
    }
    // Volatile is used as hint to the compiler that this data
    // member will be accessed by multiple threads.
    private volatile bool _shouldStop;
}

public class WorkerThreadExample : System.Web.UI.Page
{
   public static void Main()
    {
        // Create the thread object. This does not start the thread.
        Worker workerObject = new Worker();
        Thread workerThread = new Thread(workerObject.DoWork);

        // Start the worker thread.
        workerThread.Start();
        System.Web.HttpContext.Current.Response.Write("main thread: Starting worker thread...");

        // Loop until worker thread activates.
        while (!workerThread.IsAlive);

        // Put the main thread to sleep for 1 millisecond to
        // allow the worker thread to do some work:
        Thread.Sleep(1);

        // Request that the worker thread stop itself:
        workerObject.RequestStop();

        // Use the Join method to block the current thread 
        // until the object's thread terminates.
        workerThread.Join();
        System.Web.HttpContext.Current.Response.Write("main thread: Worker thread has terminated.");
    }
}
